package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.AgeService;
import com.itheima.service.SexService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/report2")
public class Report2SexAndAgeController {

    @Reference
    private SexService sexService;
    @Reference
    private AgeService ageService;
    /**
     * {
     * "data":{
     *      "sexNames":["男","女","未知"],
     *      "sexCount":[
     *          {"name":"男","value":10},
     *          {"name":"女","value":30},
     *          {"name":"未知","value":25}
     *      ]
     * },
     * "flag":true,
     * "message":"获取套餐统计数据成功"
     * }
     */
    @RequestMapping("/getSexReport")
    public Result getSexReport(){
        Map<String, Object> data = new HashMap<>();

        try {
            List<Map<String, Object>> sexCount = sexService.findSexCount();
            data.put("sexCount", sexCount);

            List<String> sexNames = new ArrayList<>();

            for (Map<String, Object> map : sexCount) {
                String name = (String) map.get("name");
                sexNames.add(name);
            }
            data.put("sexNames", sexNames);
            return new Result(true, MessageConstant.GET_SEX_COUNT_REPORT_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SEX_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/getAgeReport")
    public Result getAgeReport(){

        try {
            Map<String, Object> data = new HashMap<>();
            List<Map<String, Object>> ageCount = new ArrayList<>();
            List<String> ageNames = new ArrayList<>();
            //查询数据库返回结果
            Map<String, Object> map = ageService.findAgeCount();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Map<String, Object> mid = new HashMap<>();
                mid.put("name", entry.getKey());
                mid.put("value",entry.getValue());
                ageNames.add(entry.getKey());
                ageCount.add(mid);
            }
            data.put("ageNames", ageNames);
            data.put("ageCount", ageCount);


            return new Result(true, MessageConstant.GET_AGE_COUNT_REPORT_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_AGE_COUNT_REPORT_FAIL);
        }
    }

}
